﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using BUS;
using Entity;
using DevExpress.XtraGrid.Localization;
using DevExpress.XtraEditors.Controls;
using Share;

namespace SLMNG
{
    public partial class CA_Materials : BaseForm
    {
        #region Constructor

        public CA_Materials()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
            StatusOfForm = FormStatus.Add;            
        }

        #endregion

        #region Form Event

        void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                GridLocalizer.Active = new MyGridLocalizer();
                LoadDataGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(HandleException.HandleSQLException(ex.Message), this.Text);
            }
        }

        private void LoadDataGrid()
        {
            string[] arg = new string[] { "MaterialCode" };
            object[] obj = new object[] { DBNull.Value };
            DataSet ds1 = CatalogueBase.GetData(Material.STORE_SELECT, arg, obj);
            gridView.DataSource = ds1.Tables[0];
        }

        #endregion

        #region Override Methods

        public override void fncInsert()
        {
            tabDanhMuc111.SelectedTabPageIndex = 1;
            ActionOnControl(new Control[] { txtCode }, true);
            StatusOfForm = FormStatus.Add;
            ClearForm();
        }        

        public override void fncSave()
        {
            if (tabDanhMuc111.SelectedTabPageIndex == 1)
            {
                if (string.IsNullOrEmpty(txtCode.Text))
                {
                    MessageBox.Show(temp.ID_IS_NOT_NULL);
                    return;
                }
                else if (string.IsNullOrEmpty(txtName.Text))
                {
                    MessageBox.Show(temp.NAME_IS_NOT_NULL);
                    return;
                }

                string[] arg = new string[] { "MaterialCode", "MaterialName" };
                object[] obj = new object[] { txtCode.Text.Trim(), txtName.Text.Trim() };
                string result = string.Empty;
                if(StatusOfForm == FormStatus.Add)
                    result = CatalogueBase.SaveData(Material.STORE_INSERT, arg, obj);
                else if(StatusOfForm == FormStatus.Edit)
                    result = CatalogueBase.SaveData(Material.STORE_UPDATE, arg, obj);

                if (result == "0")
                {
                    if (StatusOfForm == FormStatus.Add)
                    {
                        ClearForm();
                        MessageBox.Show(temp.STATUS_INSERT_SUCCESS,this.Text);
                    }
                    else
                        MessageBox.Show(temp.STATUS_UPDATE_SUCCESS, this.Text);
                }
                else
                    MessageBox.Show(HandleException.HandleSQLException(result),this.Text);
            }
        }

        private void ClearForm()
        {
            txtCode.Text = string.Empty;
            txtName.Text = string.Empty;
            txtCode.Focus();
        }

        public override void fncEdit()
        {
            if (gridView1.FocusedRowHandle >= 0)
            {
                tabDanhMuc111.SelectedTabPageIndex = 1;
                ActionOnControl(new Control[] { txtCode }, false);
                StatusOfForm = FormStatus.Edit;

                string[] arg = new string[] { "MaterialCode" };
                object[] obj = new object[] { gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MaterialCode").ToString() };
                DataTable dt = CatalogueBase.GetData(Material.STORE_SELECT, arg, obj).Tables[0];
                txtCode.Text = dt.Rows[0]["MaterialCode"].ToString();
                txtName.Text = dt.Rows[0]["MaterialName"].ToString();
            }
        }

        public override void fncDelete()
        {
            if (gridView1.FocusedRowHandle >= 0)
            {
                MessageBox.Show(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MaterialCode").ToString());
            }
        }

        public override void fncRefresh()
        {
            LoadDataGrid();
        }

        #endregion

    }
}
